<template>
  <div class="wrapper">
    <img
      src="http://www.dell-lee.com/imgs/vue3/user.png"
      alt=""
      class="warpper__img"
    />
    <div class="wrapper__input">
      <input
        type="text"
        class="wrapper__input__content"
        placeholder="请输入手机号"
      />
    </div>
    <div class="wrapper__input">
      <input
        type="password"
        class="wrapper__input__content"
        placeholder="请输入密码"
      />
    </div>
    <div class="wrapper__input">
      <input
        type="password"
        class="wrapper__input__content"
        placeholder="确认密码"
        v-model="enurment"
      />
    </div>
    <div class="wrapper__register__button" @click="handelRegister">
      立即注册
    </div>
    <div class="wrapper__register__link" @click="handelLink">
      已有账号去登陆
    </div>
  </div>
  <Toast v-if="show" :message="toastMessage" />
</template>
<script>
import { useRouter } from 'vue-router'
import { post } from '../../utils/request'
import { reactive, toRefs } from 'vue'
import Toast, { useToastEffect } from '../../components/Toast.vue'

const useRegsiterEffect = () => {
  const router = useRouter()
  const data = reactive({ username: '', password: '', enurment: '' })
  const handelRegister = async showToast => {
    try {
      const reslut = await post('api/user/register', {
        username: data.username,
        password: data.password
      })
      if (reslut?.errno === 0) {
        router.push('/login')
      } else {
        showToast('注册失败')
      }
    } catch (error) {
      showToast('请求失败')
    }
  }
  const { username, password, enurment } = toRefs(data)
  return { handelRegister, username, password, enurment }
}
export default {
  name: 'Register',
  components: { Toast },
  setup(props) {
    const router = useRouter()
    const { showToast, show, toastMessage } = useToastEffect()
    const { handelRegister, username, password, enurment } = useRegsiterEffect(
      showToast
    )
    const handelLink = () => {
      router.push('/login')
    }
    return {
      handelRegister,
      handelLink,
      show,
      toastMessage,
      username,
      password,
      enurment
    }
  }
}
</script>
<style lang="scss" scoped>
@import '../../style/common.scss';

.wrapper {
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  transform: translateY(-50%);
  padding: 0 0.4rem;
  box-sizing: border-box;
  .warpper__img {
    display: block;
    width: 0.66rem;
    height: 0.66rem;
    margin: 0 auto;
    margin-bottom: 0.4rem;
  }
  .wrapper__input {
    width: 100%;
    height: 0.48rem;
    background: #f9f9f9;
    border: 0.01rem solid rgba(0, 0, 0, 0.1);
    border-radius: 0.06rem;
    padding-left: 0.16rem;
    padding-right: 0.16rem;
    box-sizing: border-box;
    margin-bottom: 0.16rem;
    input {
      width: 100%;
      height: 100%;
      background: #f9f9f9;
      border: 0;
      outline: 0;
      font-size: 0.16rem;
      color: $content-notice-fontColor;
    }
  }
  .wrapper__register__button {
    width: 100%;
    background: #0091ff;
    box-shadow: 0 0.04rem 0.08rem 0 rgba(0, 145, 255, 0.32);
    border-radius: 0.04rem;
    line-height: 0.48rem;
    font-size: 0.16rem;
    color: $bgColor;
    text-align: center;
    margin-top: 0.32rem;
    margin-bottom: 0.16rem;
    letter-spacing: 0.05rem;
    box-sizing: border-box;
  }
  .wrapper__register__link {
    text-align: center;
    font-size: 0.14rem;
    color: $content-notice-fontColor;
  }
}
</style>
